function new_player(player_name) {
	if (player_name != "") {
		player_name = player_name.replace(" ", "_");
		player_box = $('<div id="'+player_name+'_box" class="player_box ui-widget-header">');
		$("<span>", {
			class: "ui-icon ui-icon-close",
		}).click(function(){
			$("#"+player_name+"_box").fadeOut("fast", function(){
				$("#"+player_name+"_box").remove();
			});
		}).appendTo(player_box);
		$('<p>', {
			class: "player_name",
			text: player_name.replace("_", " ")
		}).appendTo(player_box);
		div_poison = $('<div class="div_poison">').appendTo(player_box);
		$('<div>', {
			class: "poison",
			id: player_name+"_poison",
			text: "0"
		}).appendTo(div_poison);
		$('<button>', {
			id: "btn_"+player_name+"_poison_plus",
			text: "+"
		}).button({
			text: false,
			icons: {primary: "ui-icon-plus"}
		}).click([player_name, +1], changePoison).appendTo(div_poison);
		$('<br>').appendTo(div_poison);
		$('<button>', {
			id: "btn_"+player_name+"_poison_minus",
			text: "-"
		}).button({
			text: false,
			icons: {primary: "ui-icon-minus"}
		}).click([player_name, -1], changePoison).appendTo(div_poison);
		div_life = $('<div class="div_life">').appendTo(player_box);
		$('<div>', {
			class: "life",
			id: player_name+"_life",
			text: "20"
		}).appendTo(div_life);
		$('<button>', {
			id: "btn_"+player_name+"_life_m1",
			class: "lifebtn",
			text: "-1"
		}).button().click([player_name, -1], changeLife).appendTo(div_life);
		$('<button>', {
			id: "btn_"+player_name+"_life_m5",
			class: "lifebtn",
			text: "-5"
		}).button().click([player_name, -5], changeLife).appendTo(div_life);
		$('<br>').appendTo(div_life);
		$('<button>', {
			id: "btn_"+player_name+"_life_p1",
			class: "lifebtn",
			text: "+1"
		}).button().click([player_name, +1], changeLife).appendTo(div_life);
		$('<button>', {
			id: "btn_"+player_name+"_life_p5",
			class: "lifebtn",
			text: "+5"
		}).button().click([player_name, +5], changeLife).appendTo(div_life);
		player_box.appendTo($("#players"));
	}
}

function removeFromGame(player_box, remove) {
	if (remove) {
		$("#"+player_box).addClass("loser");
		$("#"+player_box).animate({
			backgroundColor: "#333"
		});
	} else {
		$("#"+player_box).removeClass("loser");
		$("#"+player_box).animate({
			backgroundColor: "#CCC"
		});
	}
}

function changePoison(e) {
	player_name = e.data[0];
	value = e.data[1];
	poison_div = $("#"+player_name+"_poison");
	new_value = parseInt(poison_div.text())+value;
	if (new_value < 0) new_value = 0;
	poison_div.text(new_value);
	if (poison_div.text() >= 10) {
		removeFromGame(player_name+"_box", true);
	} else {
		removeFromGame(player_name+"_box", false);
	}
}

function changeLife(e) {
	player_name = e.data[0];
	value = e.data[1];
	life_div = $("#"+player_name+"_life");
	life_div.text(parseInt(life_div.text())+value);
	if (parseInt(life_div.text()) <= 0) {
		removeFromGame(player_name+"_box", true);
	} else {
		removeFromGame(player_name+"_box", false);
	}
}

function rollDice(values) {
	result = values[Math.floor(Math.random() * values.length)];
	$("#dialog p").text(result);
	$("#dialog").dialog();
}

function orderPlayers() {
	temp = $("<div>");
	$("#teste").empty();
	while ($("#players").children(".player_box").length > 0) {
		index = Math.ceil(Math.random()*$("#players").children(".player_box").length);
		temp.append($("#players .player_box:nth-child("+index+")"));
	}
	temp.children().each(function(index){
		$(this).appendTo($("#players"));
	});
}

function resetGame() {
	$(".player_box > div > .poison").each(function(){
		$(this).text("0");
	});
	$(".player_box > div > .life").each(function(){
		$(this).text("20");
	});
	$(".player_box").each(function(){
		removeFromGame($(this).attr("id"), false);
	});
}

$(document).ready(function(){
	$("#btn_add_player").button({icons:{primary: "ui-icon-circle-plus"}
	}).click(function(){
		$("#tb_roldice").hide();
		$("#txt_playername").val("");
		$("#tb_playername").toggle("slide", {direction: "up"}, "fast", function(){
			$("#txt_playername").focus();
		});
	});
	$("#btn_roll_dice").button({icons:{primary: "ui-icon-shuffle"}
	}).click(function(){
		$("#tb_playername").hide();
		$("#tb_roldice").toggle("slide", {direction: "up"}, "fast");
	});
	$("#btn_moeda").button().click(function(){
		$("#tb_roldice").hide("slide", {direction: "up"}, "fast");
		rollDice(["Cara", "Coroa"]);
	});
	$("#btn_d6").button().click(function(){
		$("#tb_roldice").hide("slide", {direction: "up"}, "fast");
		rollDice([1, 2, 3, 4, 5, 6]);
	});
	$("#btn_playorder").button().click(function(){
		$("#tb_roldice").hide("slide", {direction: "up"}, "fast");
		orderPlayers();
	});
	$("#btn_new_game").button({icons:{primary: "ui-icon-newwin"}
	}).click(function(){
		$(".toolbar_submenu").each(function(){
			if ($(this).css("display") != "none") {
				$(this).hide("slide", {direction: "up"}, "fast");
			}
		});
		resetGame();
	});
    $("#div_gametype").buttonset();
    $("#slider_text").slider({
    	min: 12,
    	max: 40,
    	value: 16,
    	slide: function(event, ui){
    		$(".player_box").css("font-size", ui.value);
    	}
    });
	
	$("#btn_playername").button({
		text: false,
		icons: {primary: "ui-icon-check"}
	}).click(function(){
		$("#tb_playername").hide("slide", {direction: "up"}, "fast");
		new_player($("#txt_playername").val())
	});
	
	$("#txt_playername").keypress(function(e){
		code= (e.keyCode ? e.keyCode : e.which);
		if (code == 13) {
			$("#tb_playername").hide("slide", {direction: "up"}, "fast");
			new_player($("#txt_playername").val());
		}
	});
});
